/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing.jee.web;

import com.vaadin.Application;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import testing.jee.ejb.RequestBean;
import testing.jee.web.view.AssignWorkpackageView;
import testing.jee.web.view.NewUserView;
import testing.jee.web.view.NewWorkpackageView;
import testing.jee.web.view.WeekView;

/**
 *
 * @author nova
 */
@org.springframework.stereotype.Component("vaadinApp")
@Scope(value="session")
public class VaadinApp extends Application {

    @Autowired
    RequestBean req;
    @Autowired
    WeekView weekView;
    @Autowired
    NewWorkpackageView workpackageView;
    @Autowired
    NewUserView userView;
    @Autowired
    AssignWorkpackageView assignView;
    private static final int MENU_BAR_ROW = 0;
    private static final int VIEW_ROW = 1;
    final VerticalLayout mainLayout = new VerticalLayout();
    private final MenuBar menuBar = new MenuBar();
    
    private class SwitchViewCommand implements MenuBar.Command {

        private final Component view;

        public SwitchViewCommand(Component view) {
            this.view = view;
        }
        
        @Override
        public void menuSelected(MenuItem selectedItem) {
            Component component = mainLayout.getComponent(VIEW_ROW);
            if (component != null)
                mainLayout.removeComponent(component);
            if (view instanceof View)
                ((View)view).preView();
            mainLayout.addComponent(view, VIEW_ROW);
            if (view instanceof View)
                ((View)view).postView();
        }
        
    }

    @Override
    public void init() {
        setTheme("fancydesign");
        setLocale(Locale.GERMAN);
        initLayout();
    }

    private void initLayout() {
        Window mainWindow = new Window("RP - Resource Planner");

        createMenu();
        
        mainLayout.setWidth("1024px");
        mainLayout.addStyleName("wp-center");
        
        workpackageView.preView();
        mainLayout.addComponent(workpackageView, VIEW_ROW);
        workpackageView.postView();

        HorizontalLayout centeringLayout = new HorizontalLayout();
        centeringLayout.setWidth("100%");
        centeringLayout.addComponent(new Embedded());
        centeringLayout.addComponent(mainLayout);
        centeringLayout.setComponentAlignment(mainLayout, Alignment.TOP_CENTER);
        centeringLayout.addComponent(new Embedded());
        mainWindow.addComponent(centeringLayout);
        setMainWindow(mainWindow);
    }

    private void createMenu() {
        menuBar.addItem("New", new ThemeResource("../runo/icons/16/document-add.png"), new SwitchViewCommand(workpackageView));
        menuBar.addItem("Assign", new ThemeResource("../runo/icons/16/calendar.png"), new SwitchViewCommand(assignView));
        menuBar.addItem("Overview", new ThemeResource("../runo/icons/16/globe.png"), new SwitchViewCommand(weekView));
        menuBar.addItem("Users", new ThemeResource("../runo/icons/16/users.png"), new SwitchViewCommand(userView));
        menuBar.setWidth("100%");
        mainLayout.addComponent(menuBar, MENU_BAR_ROW);
    }
}
